package com.richman.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.richman.controller.DecreaseRatioClient;
import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.SendData;
import com.richman.utils.Common;
import com.richman.utils.SoundThread;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;


public class BetWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 16325L;
	private JPanel contentPane;
	private JTextField hgBetAmtTextField;
	private JTextField ps38BetAmtTextField;
	
	private JLabel betTypelb;
	private JLabel betLeglb;
	private JLabel betMatchlb;
	private JLabel hgTeamlb;
	private JLabel ps38Teamlb;
	private JLabel hgMinbetlb;
	private JLabel ps38Minbetlb;
	private JLabel hgMaxbetlb;
	private JLabel ps38Maxbetlb;
	private JLabel hgBetAmtlb;
	private JLabel ps38BetAmtlb;
	private JLabel hgpankoulb;
	private JLabel ps38pankoulb;
	
	private JLabel messagelb;


	private SendData betgame;
	private String bettype = "";
	private String updown = "";
	
	
	private String hgpankoustr = "";
	private String ps38pankoustr = "";
	
	private String hgbetratio = "";
	
	
	private JLabel mergeRatio;
	
	private long lastbetsuccessTime = 0L;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BetWindow frame = new BetWindow();
					frame.setTitle("投注");
					frame.betTypelb.setText("单式全场让球");
					
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BetWindow() {
		
		setTitle("投注窗口");
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);  
		setBounds(100, 100, 598, 599);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		betTypelb = new JLabel("New label");
		betTypelb.setBounds(23, 27, 213, 15);
		betTypelb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(betTypelb);
		
		betLeglb = new JLabel("New label");
		betLeglb.setBounds(23, 66, 213, 15);
		betLeglb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(betLeglb);
		
		betMatchlb = new JLabel("New label");
		betMatchlb.setBounds(23, 103, 213, 15);
		betMatchlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(betMatchlb);
		
		hgTeamlb = new JLabel("New label");
		hgTeamlb.setFont(new Font("宋体", Font.PLAIN, 18));
		hgTeamlb.setBounds(23, 156, 213, 15);
		contentPane.add(hgTeamlb);
		
		ps38Teamlb = new JLabel("New label");
		ps38Teamlb.setBounds(307, 156, 213, 15);
		ps38Teamlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38Teamlb);
		
		hgMinbetlb = new JLabel("New label");
		hgMinbetlb.setBounds(23, 237, 213, 15);
		hgMinbetlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgMinbetlb);
		
		ps38Minbetlb = new JLabel("New label");
		ps38Minbetlb.setBounds(307, 237, 213, 15);
		ps38Minbetlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38Minbetlb);
		
		hgMaxbetlb = new JLabel("New label");
		hgMaxbetlb.setBounds(23, 282, 213, 15);
		hgMaxbetlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgMaxbetlb);
		
		ps38Maxbetlb = new JLabel("New label");
		ps38Maxbetlb.setBounds(307, 282, 213, 15);
		ps38Maxbetlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38Maxbetlb);
		
		
		
		
		hgpankoulb = new JLabel("New label");
		hgpankoulb.setBounds(23, 199, 213, 15);
		hgpankoulb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgpankoulb);
		
		ps38pankoulb = new JLabel("New label");
		ps38pankoulb.setBounds(307, 199, 213, 15);
		ps38pankoulb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38pankoulb);
		
		JButton refreshBtn = new JButton("刷新");
		refreshBtn.setBounds(23, 385, 109, 34);
		contentPane.add(refreshBtn);
		
		
		
		mergeRatio = new JLabel("New label");
		mergeRatio.setBounds(23, 452, 213, 15);
		mergeRatio.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(mergeRatio);
		
		
		refreshBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				refreshOdds();
				
			}
		});
		
		
		
		
		
		
		hgBetAmtlb = new JLabel("投注金额：");
		hgBetAmtlb.setBounds(23, 335, 90, 15);
		hgBetAmtlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgBetAmtlb);
		
		ps38BetAmtlb = new JLabel("投注金额：");
		ps38BetAmtlb.setBounds(307, 335, 90, 15);
		ps38BetAmtlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38BetAmtlb);
		
		hgBetAmtTextField = new JTextField();
		hgBetAmtTextField.setBounds(130, 332, 160, 30);
		hgBetAmtTextField.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgBetAmtTextField);
		hgBetAmtTextField.setColumns(10);
		
		ps38BetAmtTextField = new JTextField();
		ps38BetAmtTextField.setColumns(10);
		ps38BetAmtTextField.setFont(new Font("宋体", Font.PLAIN, 18));
		ps38BetAmtTextField.setBounds(400, 332, 160, 30);
		contentPane.add(ps38BetAmtTextField);
		
		JButton betBtn = new JButton("投注");
		betBtn.setBounds(297, 385, 109, 34);
		contentPane.add(betBtn);
		
		
		betBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				try {
					
					if(hgpankoustr.equals("")|| ps38pankoustr.equals("")) {
						messagelb.setText("请先刷新赔率");
						messagelb.setForeground(new Color(255,0,0));
						return;
					}
					
					if(Double.parseDouble(hgpankoustr) - Double.parseDouble(ps38pankoustr) < 0.001) {	
						dobet();
						return;
					}
					
				}catch(Exception ex) {
					ex.printStackTrace();
				}
				
				messagelb.setText("投注失败");
				messagelb.setForeground(new Color(255,0,0));
				
			}
		});
		
		
		messagelb = new JLabel("New label");
		messagelb.setBounds(23, 495, 399, 21);
		messagelb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(messagelb);
		
		
		JButton exitButton = new JButton("退出");
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				setVisible(false);
				
			}
		});
		exitButton.setBounds(431, 494, 109, 34);
		contentPane.add(exitButton);

		
	}

	
	/*
	 * type:	单式全场让球
	 * updown:	1代表 HG主大+PS38客小符合条件。2代表HG客小+PS38主大符合条件
	 * betgame: 某异常比赛
	 */
	public boolean setgame(String bettypearg, String updownarg, SendData betgamearg) {
		
		
		bettype = bettypearg;
		updown = updownarg;

		betgame = betgamearg;
		

		
		
		hgpankoustr = "";
		ps38pankoustr = "";
		
		hgbetratio = "";
		
		
		return refreshOdds();

	}
	
	
	public void setmegerRatioText(String str) {
		mergeRatio.setText(str);
	}
	
	public boolean refreshOdds() {
		return true;
	}
	
	
	
	
	public void dobet() {
		
	}
	
	
	
	private boolean isInRatioRange(double hgodd, double ps38odd) {
		
		double sideMinodd = DecreaseRatioClient.mainWnd.getsideMinodd();
		double sumMinodd = DecreaseRatioClient.mainWnd.getsumMinodd();
		
		double sideMaxodd = DecreaseRatioClient.mainWnd.getsideMaxodd();
		double sumMaxodd = DecreaseRatioClient.mainWnd.getsumMaxodd();
		
		DecimalFormat df = new DecimalFormat("#.000");
		
		if(hgodd < 0) {
			hgodd = Double.parseDouble(df.format(2.0 + hgodd));
		}
		
		
		
		
		if(ps38odd < 0.0) {
			ps38odd = Double.parseDouble(df.format(2.0 + ps38odd));
		}
		
		if(hgodd + ps38odd < DecreaseRatioClient.mainWnd.getbetodd()) {
			return false;
		}
		
		
		double betodd = DecreaseRatioClient.mainWnd.getbetodd();
		
		
		if(hgodd >= sideMinodd && ps38odd >= sideMinodd && (hgodd + ps38odd) >= betodd && hgodd <= sideMaxodd && ps38odd <= sideMaxodd && (hgodd+ps38odd) <= sumMaxodd) {
			return true;
		}
		
		return false;
	}
	
	
	
}
